package org.easy.file.monitor;

import lombok.extern.slf4j.Slf4j;
import org.easy.tool.consts.EasyConstants;
import org.easy.tool.date.DateUtil;
import org.easy.tool.io.FileUtil;
import org.easy.tool.string.StrUtil;

import java.io.File;
import java.util.List;

/**
 * @Description: 文件任务
 * @Author: wxl
 * @CreateTime: 2022/12/27 13:55
 */
@Slf4j
public class FileTask implements ITask {

    /**
     * 任务ID
     */
    private String taskId;
    /**
     * 文件/文件夹路径
     */
    private String filePath;
    /**
     * 是否运行
     */
    private boolean isRunning;
    /**
     * 是否开启成功
     */
    private boolean isSuccess;
    /**
     * 启动时间
     */
    private String startTime;
    /**
     * 停止时间
     */
    private String stopTime;
    /**
     * 文件后缀
     */
    private List<String> suffixList;

    private FileMonitor fileMonitor;

    public FileTask () {
        super();
        this.taskId = EasyConstants.CONST_TASK_PREFIX + DateUtil.dateForYmd()+StrUtil.randomX();
    }

    public FileTask (String filePath) {
        this();
        this.filePath = filePath;
        File file = FileUtil.file(filePath);
        this.fileMonitor = new FileMonitor(file);
    }

    /**
     * 执行监控任务
     */
    @Override
    public boolean runTask() {
        boolean startFlag = Boolean.FALSE;
        if (isRunning) {
            try {
                this.fileMonitor.getFileAlterationMonitor().start();
                log.info("开启监控路径:[{}]的任务完毕,任务ID为:{}",this.filePath,this.taskId);
                setSuccess(true);
                setStartTime(DateUtil.localDateTime());
                startFlag = Boolean.TRUE;
            } catch (Exception e) {
                log.error("开启监控路径:[{}]的任务失败,失败原因：{}",this.filePath,e.getMessage());
            }
        }
        return startFlag;
    }

    /**
     * 暂停
     */
    @Override
    public boolean stop() {
        boolean stopFlag = Boolean.FALSE;
        if (!isRunning) {
            try {
                this.fileMonitor.getFileAlterationMonitor().stop();
                log.info("停止监控路径:[{}]的任务完毕!",this.filePath);
                setSuccess(false);
                setStopTime(DateUtil.localDateTime());
                stopFlag =  Boolean.TRUE;
            } catch (Exception e) {
                log.error("关闭监控路径:[{}]的任务失败,失败原因：{}",this.filePath,e.getMessage());
            }
        }
        return stopFlag;
    }

    /**
     * 更新任务ID
     *
     * @return String
     */
    @Override
    public String updateTaskId() {
        String newTaskId = EasyConstants.CONST_TASK_PREFIX + StrUtil.randomX();
        setTaskId(newTaskId);
        return getTaskId();
    }

    @Override
    public String getTaskId() {
        return taskId;
    }

    @Override
    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    @Override
    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public boolean isSuccess() {
        return isSuccess;
    }

    @Override
    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    @Override
    public String getStartTime() {
        return startTime;
    }

    @Override
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    @Override
    public String getStopTime() {
        return stopTime;
    }

    @Override
    public void setStopTime(String stopTime) {
        this.stopTime = stopTime;
    }

    public List<String> getSuffixList() {
        return suffixList;
    }

    public void setSuffixList(List<String> suffixList) {
        this.suffixList = suffixList;
    }

    public FileMonitor getFileMonitor() {
        return fileMonitor;
    }

    public void setFileMonitor(FileMonitor fileMonitor) {
        this.fileMonitor = fileMonitor;
    }

    public static ITask create (String filePath) {
        return new FileTask(filePath);
    }

}
